<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Session;
use think\facade\Cookie;


class Wlogcate extends BaseController
{
    protected $middleware = [
        //'app\middleware\Check' => ['except' => ['login']],
        'app\middleware\Check',
    ];
    public function index(){
        $search = $this->request->param('search');
        if(!empty($search)){
            $select = Db::table('wlogcate')->whereLike('cate_name',"%$search%")->order('sort')->paginate(2);
        }else{
        $select = Db::table('wlogcate')->order('sort')->paginate(2);
        }
        return view('index',['select'=>$select]);
    }
    public function add(){
        if($this->request->isPost()){
            var_dump($_POST);
            //'cate_name' => $_POST['cate_name'], 'sort' => $_POST['sort']
            $data = $_POST;
            $data['cate_time'] = date('Y-m-d H:i:s');
            var_dump($data);
            $res = Db::name('wlogcate')->save($data);
            if($res){
                return redirect('index');
            }else{
                return "栏目添加失败";die();
            }
        }
        return view();
    }

    public function delete(){
        $id = $this->request->param('id');
        $res = Db::table('wlogcate')->delete($id);
        if($res){
            return redirect('/Wlogcate/index');
        }else{
            return "栏目删除失败";die();
        }
    }

    public function update(){
        $id = $this->request->param('id');
        $select = Db::table('wlogcate')->where('id', $id)->find();
        if($this->request->isPost()){
           $res = Db::name('wlogcate')->where('id', $id)->update(['cate_name' => $_POST['cate_name'],'sort' => $_POST['sort']]);
           if($res){
               return redirect('/Wlogcate/index');
           }else{
               return "添加失败";die();
           }
        }
        return view('add',['select'=>$select]);
    }
}